package com.globallogic.training.javase.university.member;

import java.util.Date;
import org.apache.commons.lang.StringUtils;

/**
 * This is abstract class from which the following classes inherit:
 * 	- Student
 * 	- Teacher
 * 	- Administrator
 * @author Ivan Moisiienko
 * @version 0.2 2013/05/13
 */
public abstract class Person implements Comparable<Person> {
	
	private String lastName;
	private String firstName;
	private Date dateOfBirth;
	private String placeOfBirth;
	
	/**
	 * @param firstName First Name cannot be null or blank
	 * @param lastName Last Name cannot be null or blank
	 * @param dateOfBirth Date of birth cannot be null
	 * @param placeOfBirth
	 */
	public Person(String firstName, String lastName, 
			Date dateOfBirth, String placeOfBirth) {
		
		if (lastName == null) {
			throw new IllegalArgumentException("Last Name cannot be null");
		}
		
		if (StringUtils.isBlank(lastName)) {
			throw new IllegalArgumentException("Last Name cannot be blank");
		}
		
		if (dateOfBirth == null) {
			throw new IllegalArgumentException("Date of birth cannot be null");
		}
		
		this.firstName = firstName;
		this.lastName = lastName;
		this.dateOfBirth = dateOfBirth;
		this.placeOfBirth = placeOfBirth;
	};
	
	public String getFullName(){
		return firstName + " " + lastName;
	}
	
	public String getLastName(){
		return lastName;
	}
	
	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	
	public String getPlaceOfBirth(){
		return placeOfBirth;
	}
	
	public String toString() {
		return "[" + lastName + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + lastName.hashCode();
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		Person other = (Person) obj;
		if (dateOfBirth.equals(other.dateOfBirth)
				&& getLastName().equals(other.getLastName())) {
			return true;
		}
		return false;
	}
	
	@Override
	public int compareTo(Person otherPerson) {
		String name = this.getFullName();
		String otherName = otherPerson.getFullName();
		int compare = name.compareTo(otherName);
		
		if (compare > 0) {
			return 1;
		}		
		if (compare < 0) {
			return -1;
		}
		return 0;
	}
	
}
